package com.coke.spring.sentinel.common;
import lombok.Data;

import java.util.concurrent.TimeUnit;

/**
 * @author cwm
 * @Description 滑动窗口
 * @date 2021/9/2 上午9:59
 * @Version 1.0
 */
@Data
public class SlidingTimeWindow {
    /**
     * 单位时间分割多少块
     */
    private int slot;
    /**
     * 单位时间的次数
     */
    private int limit;
    /**
     * 单位时间
     */
    private TimeUnit timeUnit;
    /**
     * 记录滑动窗口的node
     */
    private Node lastNode;
    /**
     * 每个slot的时间段
     */
    private long slotTime;


    private void init() {
        Node currentNode = null;
        long current = System.currentTimeMillis();
        for (int i = 0; i < slot; i++) {
            if (lastNode == null) {
                lastNode = new Node(current, 0, i + 1);
                currentNode = lastNode;
            }else {
                lastNode.setNext(new Node(current,0,i+1));
                lastNode=lastNode.getNext();
            }
        }
        lastNode.setNext(currentNode);
    }
}
